package com.brainhealth.web.controller.customer;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.CustomerLevel;
import com.brainhealth.customer.service.ICustomerLevelService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 会员等级Controller
 * 
 * @author wolong
 * @date 2023-04-19
 */
@RestController
@RequestMapping("/customer/level")
public class CustomerLevelController extends BaseController
{
    @Autowired
    private ICustomerLevelService customerLevelService;

    /**
     * 查询会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('customer:level:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerLevel customerLevel)
    {
        startPage();
        List<CustomerLevel> list = customerLevelService.selectCustomerLevelList(customerLevel);
        return getDataTable(list);
    }

    /**
     * 导出会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('customer:level:export')")
    @Log(title = "会员等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerLevel customerLevel)
    {
        List<CustomerLevel> list = customerLevelService.selectCustomerLevelList(customerLevel);
        ExcelUtil<CustomerLevel> util = new ExcelUtil<CustomerLevel>(CustomerLevel.class);
        util.exportExcel(response, list, "会员等级数据");
    }

    /**
     * 获取会员等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:level:query')")
    @GetMapping(value = "/{customerLevelId}")
    public AjaxResult getInfo(@PathVariable("customerLevelId") Long customerLevelId)
    {
        return success(customerLevelService.selectCustomerLevelByCustomerLevelId(customerLevelId));
    }

    /**
     * 新增会员等级
     */
    @PreAuthorize("@ss.hasPermi('customer:level:add')")
    @Log(title = "会员等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerLevel customerLevel)
    {
        return toAjax(customerLevelService.insertCustomerLevel(customerLevel));
    }

    /**
     * 修改会员等级
     */
    @PreAuthorize("@ss.hasPermi('customer:level:edit')")
    @Log(title = "会员等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerLevel customerLevel)
    {
        return toAjax(customerLevelService.updateCustomerLevel(customerLevel));
    }

    /**
     * 删除会员等级
     */
    @PreAuthorize("@ss.hasPermi('customer:level:remove')")
    @Log(title = "会员等级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerLevelIds}")
    public AjaxResult remove(@PathVariable Long[] customerLevelIds)
    {
        return toAjax(customerLevelService.deleteCustomerLevelByCustomerLevelIds(customerLevelIds));
    }

    /**
     * 获取所有的会员等级相关信息
     * @return
     */
    @Anonymous
    @PostMapping("/getCustomerLevelList")
    public AjaxResult getCustomerLevelList()
    {
        return AjaxResult.success(customerLevelService.selectCustomerLevelList(null));
    }

    /**
     * 用会员等级值获取等级信息
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/getCustomerLevelByValue")
    public AjaxResult getCustomerLevelByValue(@RequestBody Map<String, Object> map)
    {
        Long levelValue = Long.parseLong(map.get("levelValue").toString());
        CustomerLevel customerLevel = customerLevelService.selectCustomerLevelByValue(levelValue);
        if (customerLevel != null)
        {
            return AjaxResult.success("查询会员等级成功！",customerLevel);
        }
        return AjaxResult.error("查询会员等级失败！");
    }
}
